package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    ProductFeignClient productFeignClient;

    @Autowired
    TemplateEngine templateEngine;



    @GetMapping("/")
    public String index(Model model){

        model.addAttribute("list",getData());

        return "index/index";
    }

    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        Context context = new Context();
        context.setVariable("list",getData());

        try {
            Writer writer = new PrintWriter("D:\\666\\index.html","utf-8");
            templateEngine.process("index/index",context,writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    public List<Map> getData(){
        List<Map> maps = new ArrayList<>();

        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //根据1级分类Id分组，遍历放入map中  getBaseCategoryViewList
        Map<Long, List<BaseCategoryView>> baseCategoryView1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        baseCategoryView1Map.forEach((k1,v1) -> {
            Map c1Id = new HashMap();
            c1Id.put("index",k1);
            c1Id.put("categoryId",k1);
            c1Id.put("categoryName",v1.get(0).getCategory1Name());
            //根据2级分类Id分组，遍历放入map中
            Map<Long, List<BaseCategoryView>> baseCategoryView2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map> list2 = new ArrayList<>();
            baseCategoryView2Map.forEach((k2,v2) -> {
                Map c2Id = new HashMap();
                c2Id.put("categoryId",k2);
                c2Id.put("categoryName",v2.get(0).getCategory2Name());
                //把查询到的3级分类信息，遍历放入map中
                List<Map> list3 = new ArrayList<>();
                v2.forEach(v3 ->{
                    Map c3Id = new HashMap();
                    c3Id.put("categoryId",v3.getCategory3Id());
                    c3Id.put("categoryName",v3.getCategory3Name());
                    list3.add(c3Id);
                });

                c2Id.put("categoryChild",list3);
                list2.add(c2Id);
            });

            c1Id.put("categoryChild",list2);
            maps.add(c1Id);
        });
        return maps;
    }
}
